package Beginning;

import java.awt.EventQueue;
import java.awt.TextField;
import java.awt.Window;

import javax.swing.JFrame;
import javax.swing.JTable;

import java.awt.BorderLayout;

import net.miginfocom.swing.MigLayout;

import java.awt.GridLayout;

import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Form2 {

	private JFrame frame;
	private JTable table_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Form2 window = new Form2();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Form2() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		table_1 = new JTable();        		
		table_1.setBounds(10, 11, 414, 88);
		frame.getContentPane().add(table_1);
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Test();
			}
		});
		btnNewButton.setBounds(173, 121, 89, 23);
		frame.getContentPane().add(btnNewButton);
		
		
	}
	
	public DefaultTableModel map(ResultSet resultSet) throws SQLException
	{
	    DefaultTableModel defaultTableModel = new DefaultTableModel();

	    ResultSetMetaData meta = resultSet.getMetaData();
	    int numberOfColumns = meta.getColumnCount();
	    while (resultSet.next())
	    {
	        Object [] rowData = new Object[numberOfColumns];
	        for (int i = 0; i < rowData.length; ++i)
	        {
	            rowData[i] = resultSet.getObject(i+1);
	        }
	        defaultTableModel.addRow(rowData);
	    }

	    return defaultTableModel;
	}
	
	public void Test() {

		Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/feedback";
        String user = "root";
        String password = "root";

        try {
    		Class.forName("com.mysql.jdbc.Driver").newInstance();
    	}
    	catch(Exception ex) {
    		Logger lgr = Logger.getLogger(Form2.class.getName());
            lgr.log(Level.WARNING, ex.getMessage(), ex);
    	}
        try {        	
        	DefaultTableModel dtm = new DefaultTableModel();
	    	dtm.addColumn("First Name");
	        dtm.addColumn("Last Name");
	        dtm.addColumn("Years");
	        dtm.addColumn("First Name");
	        dtm.addColumn("Last Name");
	        dtm.addColumn("Years");
	        dtm.addColumn("First Name");

            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM comments");           	        
            ResultSetMetaData meta = rs.getMetaData();            
            int numberOfColumns = meta.getColumnCount();
            while (rs.next()){
            	// Set data for table
                Object [] rowData = new Object[numberOfColumns];
                for (int i = 0; i < rowData.length; ++i){
                    rowData[i] = rs.getObject(i+1);
                }
                dtm.addRow(rowData);
            }     
           
            table_1.setModel(dtm);
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Form2.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(Form2.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
    }
	
	public void testResultSet(ResultSet res){
        try{
            while(res.next()){
                System.out.println("Product ID: "+ res.getInt("product_id"));
                System.out.println("Product name: "+ res.getString("product_name"));
                System.out.println("Supplier: "+ res.getString("supplier"));
            }        
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}
